package cn.ekuma.epos.crm;

import java.util.Date;
import java.util.Properties;

import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.util.Hashcypher;

import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import cn.ekuma.epos.crm.model.CustomerStoreValueCardTableModel;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CustomerStoreValueCardEditor extends AbstractChildDAOJEditor<CustomerStoreValueCard,String>{
	private JTextField txtCardNum;
	private JTextField txtCreateDate;
	private JTextField txtValidityDate;
	private JTextField txtCustomerId;
	private JTextField txtTotalRecharge;
	private JTextField txtCurLimit;
	private JTextField txtTotalConsumer;
	private JTextField txtPassword;
	private JButton btnValidityDate;
	private JTextField txtBackLimit;
	private JButton btnNewButton;
	private JTextArea txtMemo;
	private JTextField txtState;

	public CustomerStoreValueCardEditor(DirtyManager dirty) {
		super(dirty);
		initComponents();
		txtCardNum.getDocument().addDocumentListener(dirty);
		txtValidityDate.getDocument().addDocumentListener(dirty);
		btnValidityDate.addActionListener(dirty);
		txtPassword.getDocument().addDocumentListener(dirty);
		txtBackLimit.getDocument().addDocumentListener(dirty);
		
	}
	private void initComponents() {
		setPreferredSize(new Dimension(442, 402));
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.cardNum"));
		lblNewLabel.setBounds(27, 21, 91, 15);
		add(lblNewLabel);
		
		txtCardNum = new JTextField();
		txtCardNum.setBounds(128, 18, 115, 21);
		add(txtCardNum);
		txtCardNum.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel(AppLocal.getIntString("label.createTime"));
		lblNewLabel_1.setBounds(27, 55, 91, 15);
		add(lblNewLabel_1);
		
		txtCreateDate = new JTextField();
		txtCreateDate.setEditable(false);
		txtCreateDate.setBounds(128, 52, 182, 21);
		add(txtCreateDate);
		txtCreateDate.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.validityDate"));
		lblNewLabel_2.setBounds(27, 89, 91, 15);
		add(lblNewLabel_2);
		
		txtValidityDate = new JTextField();
		txtValidityDate.setBounds(128, 86, 183, 21);
		add(txtValidityDate);
		txtValidityDate.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.state"));
		lblNewLabel_3.setBounds(253, 21, 78, 15);
		add(lblNewLabel_3);
		
		txtState = new JTextField();
		txtState.setEnabled(false);
		txtState.setBounds(329, 18, 92, 21);
		add(txtState);
		
		JLabel lblNewLabel_4 = new JLabel(AppLocal.getIntString("label.customer"));
		lblNewLabel_4.setBounds(27, 174, 91, 15);
		add(lblNewLabel_4);
		
		txtCustomerId = new JTextField();
		txtCustomerId.setEditable(false);
		txtCustomerId.setBounds(128, 171, 171, 21);
		add(txtCustomerId);
		txtCustomerId.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.totalRecharge"));
		lblNewLabel_5.setBounds(27, 207, 91, 15);
		add(lblNewLabel_5);
		
		txtTotalRecharge = new JTextField();
		txtTotalRecharge.setEditable(false);
		txtTotalRecharge.setBounds(128, 204, 92, 21);
		add(txtTotalRecharge);
		txtTotalRecharge.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.curLimit"));
		lblNewLabel_6.setBounds(27, 117, 91, 15);
		add(lblNewLabel_6);
		
		txtCurLimit = new JTextField();
		txtCurLimit.setEditable(false);
		txtCurLimit.setBounds(128, 114, 92, 21);
		add(txtCurLimit);
		txtCurLimit.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.totalConsumer"));
		lblNewLabel_7.setBounds(230, 207, 70, 15);
		add(lblNewLabel_7);
		
		txtTotalConsumer = new JTextField();
		txtTotalConsumer.setEditable(false);
		txtTotalConsumer.setBounds(310, 204, 111, 21);
		add(txtTotalConsumer);
		txtTotalConsumer.setColumns(10);
		
		JLabel label = new JLabel(AppLocal.getIntString("Label.Password"));
		label.setBounds(27, 146, 91, 15);
		add(label);
		
		txtPassword = new JTextField();
		txtPassword.setEditable(false);
		txtPassword.setBounds(128, 145, 171, 21);
		add(txtPassword);
		txtPassword.setColumns(10);
		
		txtMemo = new JTextArea();
		txtMemo.setBorder(new TitledBorder(null, AppLocal.getIntString("label.notes"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		txtMemo.setBounds(27, 243, 394, 104);
		add(txtMemo);
		
		btnValidityDate = new JButton("");
		btnValidityDate.setIcon(new ImageIcon(CustomerStoreValueCardEditor.class.getResource("/com/openbravo/images/date.png")));
		btnValidityDate.setBounds(317, 85, 33, 23);
		btnValidityDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jValidityDateActionPerformed(e);
			}
		});
		add(btnValidityDate);
		
		JLabel label_1 = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.backLimit"));
		label_1.setBounds(230, 117, 80, 15);
		add(label_1);
		
		txtBackLimit = new JTextField();
		txtBackLimit.setBounds(327, 114, 94, 21);
		add(txtBackLimit);
		txtBackLimit.setColumns(10);
		
		btnNewButton = new JButton(AppLocal.getIntString("button.peoplepassword"));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jButton1ActionPerformed(e);
			}
		});
		btnNewButton.setBounds(311, 142, 70, 23);
		add(btnNewButton);
	}

	protected void jValidityDateActionPerformed(ActionEvent e) {
		 Date date;
	        try {
	            date = (Date) Formats.TIMESTAMP.parseValue(txtValidityDate.getText());
	        } catch (BasicException ex) {
	            date = null;
	        }
	        date = JCalendarDialog.showCalendarTimeHours(this, date);
	        if (date != null) {
	        	txtValidityDate.setText(Formats.TIMESTAMP.formatValue(date));
	    }
	}
	@Override
	public String getTabName() {
		return AppLocal.getIntString("label.CustomerStoreValueCard");
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}
	
	 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

	        String sNewPassword = Hashcypher.changePassword(this);
	        if (sNewPassword != null) {
	        	txtPassword.setText(sNewPassword);
	        	dirty.setDirty(true);
	        }

	    }//GEN-LAST:event_jButton1ActionPerformed

	@Override
	public CustomerStoreValueCard createNew(Properties prop) {
		CustomerStoreValueCard newObj=new CustomerStoreValueCard();
		newObj.setCreateDate(new Date());
		if(parentNode!=null)
            newObj.setCustomerId(parentNode.getParentKey().getKey());
		
		return newObj;
	}

	@Override
	public void rebuild(CustomerStoreValueCard obj) throws BasicException {
		obj.setCardNum(txtCardNum.getText());
		obj.setValidityDate((Date) Formats.TIMESTAMP.parseValue(txtValidityDate.getText()));
		obj.setBackLimit((Double) Formats.CURRENCY.parseValue(txtBackLimit.getText()));
		obj.setMemo(txtMemo.getText());
		obj.setPassword(txtPassword.getText());
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtCardNum.setText(obj.getCardNum());
		txtValidityDate.setText(Formats.TIMESTAMP.formatValue(obj.getValidityDate()));
		txtPassword.setText(obj.getPassword());
		txtBackLimit.setText(Formats.CURRENCY.formatValue(obj.getBackLimit()));
		txtCreateDate.setText(Formats.TIMESTAMP.formatValue(obj.getCreateDate()));
		txtCurLimit.setText(Formats.CURRENCY.formatValue(obj.getCurLimit()));
		txtCustomerId.setText(obj.getCustomerId());
		txtTotalRecharge.setText(Formats.CURRENCY.formatValue(obj.getTotalRecharge()));
		txtTotalConsumer.setText(Formats.CURRENCY.formatValue(obj.getTotalConsumer()));
		txtMemo.setText(obj.getMemo());
		txtState.setText(CustomerStoreValueCardTableModel.getState(obj.getState()));
		
		btnValidityDate.setEnabled(editAble);
		txtCardNum.setEditable(editAble);
		txtBackLimit.setEditable(editAble);
		btnNewButton.setEnabled(editAble);
		txtMemo.setEditable(editAble);
	}

	@Override
	public Class getEditClass() {
		return CustomerStoreValueCard.class;
	}
}
